package com.nimlib;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.viewpager.widget.PagerAdapter;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.jinlejia.erp.R;
import com.jinlejia.erp.databinding.ActivityNimPictureViewPagerBinding;
import com.netease.nimlib.sdk.RequestCallbackWrapper;
import com.netease.nimlib.sdk.nos.model.NosThumbParam;
import com.nimlib.utils.NimIMUtils;

import java.io.File;
import java.util.ArrayList;

import uk.co.senab.photoview.PhotoView;

public class NimPictureViewPagerActivity extends AppCompatActivity {

    private ActivityNimPictureViewPagerBinding binding;

    public static void start(Context context, ArrayList<ImageViewBean> imgs, int position){
        Intent intent = new Intent();
        intent.setClass(context,NimPictureViewPagerActivity.class);
        intent.putExtra("imgs",imgs);
        intent.putExtra("position",position);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this,R.layout.activity_nim_picture_view_pager);
        binding.idBack.setOnClickListener(v->finish());
        Intent intent = getIntent();
        int position = intent.getIntExtra("position",0);
        ArrayList<ImageViewBean> imgs = intent.getParcelableArrayListExtra("imgs");
        binding.viewPager.setAdapter(new InnerPagerAdapter(imgs));
        binding.viewPager.setCurrentItem(position);
    }


    private class InnerPagerAdapter extends PagerAdapter {
        private ArrayList<ImageViewBean> imgs;
        InnerPagerAdapter(ArrayList<ImageViewBean> imgs){
            this.imgs = imgs;
        }
        @Override
        public int getCount() {
            return imgs.size();
        }

        @Override
        public View instantiateItem(ViewGroup container, int position) {
            PhotoView photoView = new PhotoView(container.getContext());
            ImageViewBean bean = imgs.get(position);
            String save = bean.getPathForSave();
            File file = new File(save);
            int height = bean.getHeight();
            int width = bean.getWidth();
            boolean exits = file.exists();
            if (exits){Glide.with(NimPictureViewPagerActivity.this).load(imgs.get(position)).placeholder(R.mipmap.ic_no_img).into(photoView);
                loadImage(file,photoView);
            }else {
                NosThumbParam thumb = new NosThumbParam();
                thumb.height = height;
                thumb.width = width;
                Glide.with(NimPictureViewPagerActivity.this).load(R.drawable.nim_loading).into(photoView);
                NimIMUtils.downloadImageFile(bean.getUrl(), thumb, save, new RequestCallbackWrapper() {
                    @Override
                    public void onResult(int code, Object result, Throwable exception) {
                        if (code==200){
                            loadImage(file,photoView);
                        }
                    }
                });
            }
            container.addView(photoView, width,height);
            return photoView;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        private void loadImage(File file, ImageView imageView){
            Glide.with(imageView)
                    .load(file)
                    .thumbnail(Glide.with(imageView.getContext()).load(R.drawable.nim_loading))
                    .fallback(R.mipmap.ic_no_img)
                    .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
                    .fitCenter()
                    .centerCrop()
                    .into(imageView);
        }
    }
}
